#!/usr/bin/env python
# -*- coding: utf-8 -*-

##
# This script is an exmple of using the (optional) ib.opt package
# instead of the regular API.
##

from time import sleep
from gj.opt import gjConnection, message
from gj.lib import logger

mylog = logger.logger()


def my_account_handler(msg):
    print(msg)


def historical_data_handler(msg):
    print(msg)


def error(msg):
    mylog.error(msg)


if __name__ == '__main__':

    # con.register(my_account_handler, 'UpdateAccountValue')
    # con.register(my_tick_handler, message.tickSize, message.tickPrice)
    import random

    client_id = random.randint(1, 1000000)
    con = gjConnection(clientId=client_id)
    mymessage = getattr(message, 'error')
    con.register(error, mymessage)
    # con.register(historical_data_handler, 'historicalData')
    con.register(historical_data_handler, 'liveData')
    status = con.connect()
    mylog.debug('连接状态:%s' % status)

    mylog.debug('over')
    paras = {'stock_list': ['000001.SZ', '600000.SH'],
             'start_time': '20240301', 'end_time': '20240331', 'period': '1d'}
    # con.reqHistoricalData(client_id,**paras)

    # while True:
    #     sleep(100)
    # inner()
    # sleep(5)
    # print('disconnected', con.disconnect())
    # sleep(3)
    # print('reconnected', con.reconnect())
    # inner()
    # sleep(3)

    # print('again disconnected', con.disconnect())
